package com.erpanderp.wam.tm.view;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import com.erpanderp.wam.tm.model.entity.RecruitmentVendor;
import com.erpanderp.wam.tm.model.service.VendorRegistrationDAO;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ManagedBean
@ViewScoped
public class CreateVendorRegistrationBean implements Serializable {

	
	private static final long serialVersionUID = 1L;
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	
	RecruitmentVendor recruitmentVendor=new RecruitmentVendor();
	private VendorRegistrationDAO vendorRegistrationDAO=new VendorRegistrationDAO();
	private boolean FORM_VALID;
	private boolean vendorExists = true;
	private String vendorCode ;
	
	public CreateVendorRegistrationBean() {
		if(wamUser != null){
			
			Calendar currentDate = Calendar.getInstance();
			SimpleDateFormat formate= new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
			String toDay = formate.format(currentDate.getTime());
			Date date = null;
			try {
				date = (Date)formate.parse(toDay);
			} catch (ParseException e) {
				e.printStackTrace();
			}  
			
			vendorCode=CommonLookup.getAutoGeneratedVendorCode();
			recruitmentVendor.setVendorCode(vendorCode);
			recruitmentVendor.setEmployee(wamUser.getEmployee());
		    recruitmentVendor.setCreateDate(date);
		    recruitmentVendor.setCreatedByName(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getFirstName());
		}
	}
	
	
	
	public void confirmVendorRequest(){			
		validateForm();
		if(FORM_VALID){
			RequestContext.getCurrentInstance().execute("confirmation.show();"); 
		}
	}
	
	public String saveVendorRequest(){	
		validateForm();
		
		if(FORM_VALID){
			vendorExists = vendorRegistrationDAO.isVendorCodeExistOrNot(vendorCode);
			
			if (!vendorExists){                  
				vendorCode=CommonLookup.getAutoGeneratedVendorCode();								
			}			
			
			recruitmentVendor.setCreateDate(new Date());
			recruitmentVendor.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
			recruitmentVendor.setStatus(CommonLookup.STATUS_ACTIVE);
			vendorRegistrationDAO.createRecruitmentVendor(recruitmentVendor);
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, vendorCode+" recruitment vendor created successfully.", ""));
			recruitmentVendor = new RecruitmentVendor();
			
			return "createvendorregistration";
		}
		return null;
		
	}
		
		private void validateForm(){
			FORM_VALID = true;					
		  
		   if(ValidationUtil.isEmpty(recruitmentVendor.getVendorName(), "Vendor Name ", null, FacesContext.getCurrentInstance()) != null){
				FORM_VALID = false;
			}
		   if(ValidationUtil.isEmpty(recruitmentVendor.getVendorEmail(), "Vendor E-Mail", null, FacesContext.getCurrentInstance()) != null){
				FORM_VALID = false;
			}else {
				System.out.println("EMAIL ENTERED");
				if (ValidationUtil.isEmailValid(recruitmentVendor.getVendorEmail(), "Vendor E-Mail", null,
						FacesContext.getCurrentInstance()) != null) {
					FORM_VALID = false;
				} 
			}
		   if(ValidationUtil.isEmpty(recruitmentVendor.getVendorContactNumber(), "Vendor Contact Number", null, FacesContext.getCurrentInstance()) != null){
				FORM_VALID = false;
			}
		   if(ValidationUtil.isEmpty(recruitmentVendor.getVendorContactPerson(), "Vendor Contact Person", null, FacesContext.getCurrentInstance()) != null){
				FORM_VALID = false;
			}
		   if(ValidationUtil.isEmpty(recruitmentVendor.getVendorAddress(), "Vendor Address", null, FacesContext.getCurrentInstance()) != null){
				FORM_VALID = false;
			}		  
		 
			System.out.println("Finally FORM_VALID is :::::"+FORM_VALID);		
		}
	
	//SETTERS AND GETTERS
	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public RecruitmentVendor getRecruitmentVendor() {
		return recruitmentVendor;
	}

	public void setRecruitmentVendor(RecruitmentVendor recruitmentVendor) {
		this.recruitmentVendor = recruitmentVendor;
	}
	public boolean isFORM_VALID() {
		return FORM_VALID;
	}
	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}

	public VendorRegistrationDAO getVendorRegistrationDAO() {
		return vendorRegistrationDAO;
	}

	public void setVendorRegistrationDAO(VendorRegistrationDAO vendorRegistrationDAO) {
		this.vendorRegistrationDAO = vendorRegistrationDAO;
	}

	public boolean isVendorExists() {
		return vendorExists;
	}

	public void setVendorExists(boolean vendorExists) {
		this.vendorExists = vendorExists;
	}

	public String getVendorCode() {
		return vendorCode;
	}

	public void setVendorCode(String vendorCode) {
		this.vendorCode = vendorCode;
	}

}
